<?php
class V1_news extends CI_Controller{
	 function V1_news(){
	  	parent::__construct();
	  	$this->load->model('news');
	 }
	 function index(){
	 	$this->load->view('news.index.php');
	 }
	 function detail(){
	 	$this->load->view('news.detail.php');
	 }
	 function table(){
	 	$htdocs=$this->load->get_var('htdocs');
	 	$data['cover']=$this->input->get('cover');
	 	$data['tpl']=$tpl=$this->input->get('tpl',true);
	 	$clg=intval($this->input->get('clg'));
	 	$data['tpl']=$tpl=$this->input->get('tpl',true);
	 	$data['look']=intval($this->input->get('look'));
	 	$data['cover']=$this->input->get('cover',true);
	 	$start=intval($this->input->get('srt'));
	 	$get['lth']=intval($this->input->get('lth'));$get['lth']=empty($get['lth'])?15:$get['lth'];
	 	$search='';
	 	if($this->input->get('search')){
	 		$data['search']=$search=$get['search']=$this->input->get('search',true);
	 	}
	 	if($this->input->server('REQUEST_METHOD')=='POST'){
	 		$sequence=$this->input->post('order');
	 		foreach($sequence as $key=>$value){
	 			$this->news->update(array('where'=>array('id'=>intval($key)),'set'=>array('sequence'=>intval($value))));
	 		}
	 	}
	 	$this->load->model('resource');
		$T=$this->resource->news($clg,$start,$get['lth'],$search);
		$data['news']=$T['RE'];$data['total']=$T['TD'];
		$data['basic']='/news/table.html?tpl='.$tpl.'&cover='.$data['cover'].'&clg='.$clg.'&hcs='.$htdocs['id'].'&'.http_build_query($get);
		$data['pagination']=$this->resource->pagination($data['basic'],$T['TD'],$get['lth']);
		$this->load->view('news/news.table.php',$data);
	 }
	 function write(){
	 	$htdocs=$this->load->get_var('htdocs');
	 	$data['cover']=$this->input->get('cover');
	 	$data['tpl']=$tpl=$this->input->get('tpl',true);
	 	$clg=intval($this->input->get('clg'));
	 	$news=intval($this->input->get('news'));
	 	$data['news']['release']=time();
	 	if($this->input->server('REQUEST_METHOD')=='POST'){
	 		$data['news']['catalog']=$clg;
			$data['news']['caption']=$this->input->post('caption');
			$data['news']['cover']=$this->input->post('cover');
			$data['news']['release']=strtotime($this->input->post('release'));$data['news']['release']=empty($data['news']['release'])?time():$data['news']['release'];
			$data['news']['author']=$this->input->post('author');
			$data['news']['source']=$this->input->post('source');
			$data['news']['content']=$this->input->post('content');
			$data['news']['enhancer']=intval($_SESSION['id']);
			$data['news']['date']=time();
			$data['news']['locked']=0;
			if(empty($data['news']['caption'])){
				$data['message']['caption']='标题不可为空';
			}else{
				$data['news']['id']=$this->news->insert($data['news']);
				header('Location: /news/table.html?tpl='.$tpl.'&cover='.$data['cover'].'&clg='.$clg.'&hcs='.$htdocs['id']);
		 		exit();
			}
	 	}
	 	if($news!==0){
	 		$data['news']=reset($this->news->get(array('where'=>array('id'=>$news,'locked'=>0))));
	 	}
	 	$this->load->view('news/'.$tpl.'.php',$data);
	 }
}
?>